﻿using System.Collections;
using UnityEngine;
using UnityEngine.SocialPlatforms;
using DG.Tweening;
using UnityEngine.UI;

public class Logos : MonoBehaviour
{
    [SerializeField] private Sprite _bg;
    [SerializeField] private Sprite[] _sprites;
    [SerializeField] private int _contX = 10;
    [SerializeField] private CanvasGroup _cavs;
    [SerializeField] private CanvasGroup _cavs2;
    // Use this for initialization
    void Start ()
	{
	    StartCoroutine(CtreateIcon());
	    _cavs2.gameObject.SetActive(false);
        _cavs2.alpha = 0;
	    _cavs2.transform.GetChild(0).localScale = Vector3.zero;
    }
    private IEnumerator CtreateIcon()
    {
        int i = 0;
        int x = 0;
        int y = 0;
        transform.localPosition = new Vector3(-(_contX - 1) * 0.5f*1.15f,(_sprites.Length % _contX > 0 ? 1 + _sprites.Length : _sprites.Length) / _contX * 0.5f * 1.15f);
        float logoTime = 0;
        while (i < _sprites.Length)
        {
            if (x == _contX)
            {
                logoTime += Time.deltaTime;
                x = 0;
                y++;
                yield return 0;
            }
            var go = new GameObject("icon" + i);
            go.transform.SetParent(transform);
            go.AddComponent<Icon>().Init(_sprites[i], _bg, new Vector2(x * 1.15f, -y * 1.15f), ToShowImage);
            i++;
            x++;
        }
        _cavs.DOFade(0, 0.75f);
        while (true)
        {
            Icon.Icons[Random.Range(0, Icon.Icons.Count)].Play();
            yield return new WaitForSeconds(0.11f);
        }
    }
    private static int _curIndex = 0;

    private void ToShowImage()
    {
        _cavs2.gameObject.SetActive(true);
        _cavs2.DOFade(1, Icon.TIME_DURATION);
        _cavs2.transform.GetChild(0).DOScale(Vector3.one, Icon.TIME_DURATION);
    }

    public void ToInitState()
    {
        Icon.ToAllInitState();
        _cavs2.DOFade(0, Icon.TIME_DURATION);
        _cavs2.transform.GetChild(0).DOScale(Vector3.zero, Icon.TIME_DURATION).OnComplete(() =>
        {
            _cavs2.gameObject.SetActive(false);
        });
    }
}
